<template>
  <div class="container">
    <el-card class="box-card">
      <!-- 筛选 -->
      <el-form label-width="80px" size="small">
        <el-row>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input
                style="200px"
                v-model="requestParams.keyword"
                placeholder="根据编号搜索"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="12">
            <el-form-item style="text-align: right">
              <el-button @click="clear">清除</el-button>
              <el-button type="primary" @click="searchForm">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据记录 -->
      <el-alert
        :closable="false"
        :title="`数据一共${total}条`"
        type="info"
        show-icon
      >
      </el-alert>
      <!-- 表格区域 -->
      <el-table :data="questions" style="width: 100%">
        <el-table-column label="编号" prop="id" width="200px"></el-table-column>
        <el-table-column label="题型" prop="questionType">
          <template slot-scope="scope">
            {{
              questionType.find(
                (item) => item.value === +scope.row.questionType
              ).label
            }}
          </template>
        </el-table-column>
        <el-table-column label="题目编号" prop="questionIDs" width="220px">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.questionIDs" :key="index">
              <p class="bh" @click="openPreview(scope.row, index)">
                {{ item.number }}
              </p>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="录入时间" prop="addTime"></el-table-column>
        <el-table-column
          label="答题时间(s)"
          prop="totalSeconds"
        ></el-table-column>
        <el-table-column label="正确率" prop="accuracyRate"></el-table-column>
        <el-table-column label="录入人" prop="userName"></el-table-column>
        <el-table-column label="操作" width="80px">
          <template slot-scope="scope">
            <el-button type="danger" plain icon="el-icon-delete" circle @click="removeBtn(scope.row)" title="删除"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top: 20px; text-align: right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="requestParams.pagesize"
        :current-page="requestParams.page"
        @current-change="pager"
        :page-sizes="[20, 30, 50, 100]"
        @size-change="handleSizeChange"
      ></el-pagination>
    </el-card>
    <questionsPreview ref="questionsPreview" :data="questionInfo" />
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
import { questionType } from '@/api/hmmm/constants.js'
import questionsPreview from '../components/questions-preview'
export default {
  data () {
    return {
      requestParams: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      total: 0,
      questions: [],
      questionType,
      questionInfo: {}
    }
  },

  created () {
    this.getlist()
  },
  components: {
    questionsPreview
  },
  methods: {
    async getlist () {
      const { data: res } = await randoms(this.requestParams)
      this.total = res.counts
      this.questions = res.items
      console.log(this.questions)
    },
    // 点击了试题编号
    openPreview (row, index) {
      this.questionInfo = row
      const id = this.questionInfo.questionIDs[index].id
      this.$nextTick(() => {
        this.$refs.questionsPreview.open({ id })
      })
    },
    // 点击了清除按钮
    clear () {
      for (const key in this.requestParams) {
        if (key !== 'page' && key !== 'pagesize') this.requestParams[key] = null
      }
      this.getlist()
    },
    // 点击了搜索按钮
    searchForm () {
      this.requestParams.page = 1
      this.getlist()
    },
    // 点击分页时的函数
    handleSizeChange (val) {
      this.requestParams.page = 1
      this.requestParams.pagesize = val
      this.getlist()
    },
    pager (val) {
      this.requestParams.page = val
      this.geilist()
    },
    // 点击了删除按钮
    removeBtn(row) {
      this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeRandoms({id: row.id})
        this.getlist()
        this.$message.success('删除成功')
      })
    }
  }
}
</script>

<style scoped lang='less'>
.bh {
  color: blue;
  cursor: pointer;
  user-select: none;
}
</style>

